/**
 * Copyright (c) qzsoft All rights reserved.
 *
 * qzsoft.cn
 *
 * 版权所有，侵权必究！
 */

package com.qzsoft.system.modules.permission.role.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qzsoft.system.modules.permission.role.service.SysRoleMenuService;
import com.qzsoft.system.repository.sys.dao.SysRoleMenuDao;
import com.qzsoft.system.repository.sys.entity.SysRoleMenuEntity;
import com.qzsoft.jeemis.common.service.BaseService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;


/**
 * 角色与菜单对应关系
 * 
 * @author
 */
@Service
public class SysRoleMenuServiceImpl extends BaseService  implements SysRoleMenuService {

	@Autowired
	SysRoleMenuDao sysRoleMenuDao;

//	@Autowired
//	OpmServiceApplicationService applicationService;

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveOrUpdate(Long roleId,String applicationNo, List<String> menuIdList) {
		//先删除角色菜单关系
		deleteByRoleIds(new Long[]{roleId});

		//角色没有一个菜单权限的情况
		if(CollUtil.isEmpty(menuIdList)){
			return ;
		}

		//保存角色菜单关系
		for(String menuId : menuIdList){
			SysRoleMenuEntity sysRoleMenuEntity = new SysRoleMenuEntity();
			sysRoleMenuEntity.setMenuId(menuId);
			sysRoleMenuEntity.setRoleId(roleId);
			sysRoleMenuEntity.setApplicationNo(applicationNo);

			//保存
			sysRoleMenuDao.insert(sysRoleMenuEntity);
		}
	}

	@Override
	public List<String> getMenuIdList(Long roleId,String applicationNo){
		if (StringUtils.isEmpty(applicationNo)) {
			// 获取系统列表
//			List<ServiceApplicationTreeDTO> list = applicationService.selectList();
//			if (list.size() > 0) {
//				applicationNo = list.get(0).getApplicationNo();
//			}
		}
		QueryWrapper<SysRoleMenuEntity> queryWrapper=new QueryWrapper<>();
		//queryWrapper.eq("role_id",roleId).eq("application_no",applicationNo).select("menu_id");
		queryWrapper.eq("role_id",roleId).select("menu_id");
		List<SysRoleMenuEntity> menuList=sysRoleMenuDao.selectList(queryWrapper);
		List<String> menuIdList= menuList.stream().map(obj->{return obj.getMenuId();}).collect(Collectors.toList());
		return menuIdList;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void deleteByRoleIds(Long[] roleIds) {
		QueryWrapper<SysRoleMenuEntity> queryWrapper=new QueryWrapper<>();
		queryWrapper.in("role_id",roleIds);
		sysRoleMenuDao.delete(queryWrapper);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void deleteByMenuId(String menuId) {
		QueryWrapper<SysRoleMenuEntity> queryWrapper=new QueryWrapper<>();
		queryWrapper.eq("menu_id",menuId);
		sysRoleMenuDao.delete(queryWrapper);
	}

	@Override
	public List<String> getV2MenuIdList(Long roleId, String applicationNo) {
//		if (StringUtils.isEmpty(applicationNo)) {
//			// 获取系统列表
//			List<ServiceApplicationTreeDTO> list = applicationService.selectList();
//			if (list.size() > 0) {
//				applicationNo = list.get(0).getApplicationNo();
//			}
//		}
//		QueryWrapper<SysRoleMenuEntity> queryWrapper=new QueryWrapper<>();
//		queryWrapper.eq("role_id",roleId).eq("application_no",applicationNo).select("menu_id");
//		List<SysRoleMenuEntity> menuList=sysRoleMenuDao.selectList(queryWrapper);
//		List<String> menuIdList= menuList.stream().map(obj->{return obj.getMenuId();}).collect(Collectors.toList());
//		return menuIdList;
		return  null;
	}

}